package com.example.nbshangpping.fpage;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.nbshangpping.R;
import com.example.nbshangpping.goods.TypeBean;
import com.example.nbshangpping.utils.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
public class FPTypeAdapter extends BaseAdapter {
    Context context;
    List<TypeBean.DataBean>list;
    public FPTypeAdapter(Context context, List<TypeBean.DataBean> alllist,int page,int pagesize) {
        this.context = context;
        list = new ArrayList<>();
        // 0  0-7     1  8-15    2 16-23
        int start = page*pagesize;
        int end = (page+1)*pagesize-1;
        if (end>alllist.size()-1) {
            end = alllist.size()-1;
        }
        for (int i = start; i <=end ; i++) {
            TypeBean.DataBean bean = alllist.get(i);
            list.add(bean);
        }
    }
    @Override
    public int getCount() { return list.size(); }
    @Override
    public Object getItem(int position) { return list.get(position); }
    @Override
    public long getItemId(int position) { return position; }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_fpage_type,parent,false);
            vh = new VH(convertView);
            convertView.setTag(vh);
        }else {
            vh = (VH) convertView.getTag();
        }
        TypeBean.DataBean dataBean = list.get(position);
        vh.tv.setText(dataBean.getName());
        String icon = dataBean.getIcon();
        if (!TextUtils.isEmpty(icon)) {
            String url = URLUtils.PUBLIC_URL+icon;
            Picasso.with(context).load(url).into(vh.iv);
        }
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int id = dataBean.getId();
                Intent intent = new Intent(context, CategoryActivity.class);
                intent.putExtra("id",id);
                context.startActivity(intent);
            }
        });
        return convertView;
    }
    class VH{
        ImageView iv;
        TextView tv;
        public VH(View v){
            iv = v.findViewById(R.id.item_fpage_type_iv);
            tv = v.findViewById(R.id.item_fpage_type_tv);
        }
    }
}